package com.zy.untils;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaMetadataRetriever;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.zy.base.R;

public class GlideUtils {

    /**
     * 获取音乐id3封面
     * @param view view
     * @param path path
     */
    public static void setImageView(ImageView view,String path){
               Glide.with(view)
               .load(loadCover(path))
               .placeholder(R.mipmap.default_image_round)
               .error(R.mipmap.default_image_round)
               .diskCacheStrategy(DiskCacheStrategy.NONE)
               .into(view);

    }
    /**
     * 获取video id3封面
     * @param imageView view
     * @param path path
     */
    @SuppressLint("CheckResult")
    public static void getVideoImageView(ImageView imageView, String path){
        RequestOptions options = new RequestOptions();
        options.skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL)
                .frame(1000)
                .centerCrop();

        Glide.with(imageView)
                .setDefaultRequestOptions(options)
                .load(path)
                .into(imageView);

    }
    private static Bitmap loadCover(String path) {
        if(TextUtils.isEmpty(path)){
            return null;
        }
        MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
        mediaMetadataRetriever.setDataSource(path);
        byte[] cover = mediaMetadataRetriever.getEmbeddedPicture();
        if(cover==null||cover.length==0){
            return null;
        }
        return BitmapFactory.decodeByteArray(cover, 0, cover.length);
    }
}
